/*
 * Copyright (C) 2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayoutManager;
import ohos.agp.components.ListContainer;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.multimodalinput.event.TouchEvent;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.sample.weight.MaterButton.ButtonFloat;

import java.io.IOException;

/**
 * CardAbility
 *
 * @since 2021-06-21
 */
public class CardAbility extends Ability {
    private ListContainer mRecyclerView;
    private DirectionalLayoutManager mDirectionalLayoutManager;
    private MaterialTapTargetPrompt mFabPrompt;
    private ComponentContainer mRootView;
    private ButtonFloat fab;
    private MaterialTapTargetPrompt fabPrompt;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_card);
        initViews();
    }

    private void initViews() {
        findComponentById(ResourceTable.Id_ig_back).setClickedListener(component -> terminateAbility());
        mRootView = (ComponentContainer) findComponentById(ResourceTable.Id_drawer_layout);
        fab = (ButtonFloat) findComponentById(ResourceTable.Id_fab);
        fab.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                show();
            }
        });
        showFab();

        mRecyclerView = (ListContainer) findComponentById(ResourceTable.Id_my_recycler_view);
        mRecyclerView.setLongClickable(false);
        mDirectionalLayoutManager = new DirectionalLayoutManager();
        mRecyclerView.setLayoutManager(mDirectionalLayoutManager);
        CardAdapter cardAdapter = new CardAdapter(this);
        mRecyclerView.setItemProvider(cardAdapter);
        mRootView.setTouchEventListener((component, touchEvent) -> {
            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                if (fabPrompt != null) {
                    fabPrompt.dismiss();
                    fabPrompt = null;
                }
            }
            return true;
        });
    }

    private void show() {
        if (fabPrompt != null) {
            fabPrompt.dismiss();
            fabPrompt = null;
        }
        try {
            final CardAdapter.BaseViewHolder viewHolder = (CardAdapter.BaseViewHolder)
                    mRecyclerView.getComponentAt(0).getTag();
            fabPrompt = new MaterialTapTargetPrompt.Builder(CardAbility.this, viewHolder.mDrawLayout)
                    .setTarget(viewHolder.mImageView)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setPrimaryText(getString(ResourceTable.String_example_card_card_title))
                    .setSecondaryText(getString(ResourceTable.String_example_card_card_description))
                    .setClipToView(viewHolder.mDrawLayout)
                    .setFocalPadding(ResourceTable.Float_dp40)
                    .setPromptStateChangeListener((prompt, state) -> {
                        if (state == MaterialTapTargetPrompt.STATE_FOCAL_PRESSED
                            || state == MaterialTapTargetPrompt.STATE_NON_FOCAL_PRESSED
                            || state == MaterialTapTargetPrompt.STATE_FOCAL_BACKGROUND_PRESSED) {
                            fabPrompt = null;
                        }
                    })
                    .show();
        } catch (NotExistException | IOException | WrongTypeException e) {
            String error = e.toString();
        }
    }

    private void showFab() {
        try {
            mFabPrompt = new MaterialTapTargetPrompt.Builder(this, mRootView)
                    .setTarget(fab)
                    .setFocalPadding(ResourceTable.Float_dp40)
                    .setPrimaryText(getString(ResourceTable.String_example_card_fab_title))
                    .setSecondaryText(getString(ResourceTable.String_example_card_fab_description))
                    .setBackButtonDismissEnabled(true)
                    .setBackgroundColour(getColor(ResourceTable.Color_colorAccent))
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setPromptStateChangeListener((prompt, state) -> {
                        if (state == MaterialTapTargetPrompt.STATE_FOCAL_PRESSED
                                || state == MaterialTapTargetPrompt.STATE_NON_FOCAL_PRESSED
                                || state == MaterialTapTargetPrompt.STATE_FOCAL_BACKGROUND_PRESSED) {
                            mFabPrompt = null;
                        }
                    })
                    .create();
        } catch (NotExistException | IOException | WrongTypeException e) {
            String error = e.toString();
        }
        if (mFabPrompt != null) {
            mFabPrompt.show();
        }
    }
}
